/*
 * Date          Author      Note
 * 2015.06.24    舞者无罪            16进制键盘
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "hex_key.h"
#include "flower.h"


/*
 *
 */
static void CreateHexKeyInterface(HWND hWnd)
{
    int i = 0, j = 0;
    ushort id[16] = {
        ID_1NUM, ID_2NUM, ID_3NUM, ID_4NUM,
        ID_5NUM, ID_6NUM, ID_7NUM, ID_8NUM,
        ID_9NUM, ID_0NUM, ID_10NUM, ID_11NUM,
        ID_12NUM, ID_13NUM, ID_14NUM, ID_15NUM
    };
    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 4; ++j)
        {
            CreateWindow(MH_STATIC_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                id[i * 4 + j],
                3 + (CX_DAT_0BMP + 3) * j,
                3 + (CY_DAT_0BMP + 3) * i,
                CX_DAT_0BMP,
                CY_DAT_0BMP,
                hWnd, (int)&g_bmp[BMP_1DAT + i * 4 + j]);
        }
    }
    //
    CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_DEL,
        3,
        3 + (CY_DAT_0BMP + 3) * 4,
        CX_HEX_DEL_BMP,
        CY_HEX_DEL_BMP,
        hWnd, (int)&g_bmp[BMP_HEX_DEL]);
    //
    CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        3 + (CX_HEX_DEL_BMP + 3),
        3 + (CY_DAT_0BMP + 3) * 4,
        CX_HEX_ESC_BMP,
        CY_HEX_ESC_BMP,
        hWnd, (int)&g_bmp[BMP_HEX_ESC]);
    //
    CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ENTER,
        3 + (CX_HEX_ESC_BMP + 3 + CX_HEX_DEL_BMP + 3),
        3 + (CY_DAT_0BMP + 3) * 4,
        CX_HEX_ENTER_BMP,
        CY_HEX_ENTER_BMP,
        hWnd, (int)&g_bmp[BMP_HEX_ENTER]);
}
/*
 *
 */
static void OnChar(HWND hWnd, int ch)
{
    HWND hParent = GetHosting(hWnd);
    HWND hTmp = 0;
    if(hParent > 0)
    {
        hTmp = GetFocusChild(hParent);
    }
    if(ch >= '0' && ch <= '9')
    {
        int len = strlen(g_edit_text);
        if(len < 32)
        {
            g_edit_text[len] = ch;
            g_edit_text[len + 1] = 0;
        }
        if(hTmp > 0)
        {
            SetWindowCaption(hTmp, g_edit_text);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
    else if(ch >= 'A' && ch <= 'F')
    {
        int len = strlen(g_edit_text);
        if(len < 32)
        {
            g_edit_text[len] = ch;
            g_edit_text[len + 1] = 0;
        }
        if(hTmp > 0)
        {
            SetWindowCaption(hTmp, g_edit_text);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
    else if(ch >= 'a' && ch <= 'f')
    {
        int len = strlen(g_edit_text);
        if(len < 32)
        {
            g_edit_text[len] = ch - 32;
            g_edit_text[len + 1] = 0;
        }
        if(hTmp > 0)
        {
            SetWindowCaption(hTmp, g_edit_text);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_0NUM:
        {
            OnChar(hWnd, '0');
        }
        break;
    case ID_1NUM:
        {
            OnChar(hWnd, '1');
        }
        break;
    case ID_2NUM:
        {
            OnChar(hWnd, '2');
        }
        break;
    case ID_3NUM:
        {
            OnChar(hWnd, '3');
        }
        break;
    case ID_4NUM:
        {
            OnChar(hWnd, '4');
        }
        break;
    case ID_5NUM:
        {
            OnChar(hWnd, '5');
        }
        break;
    case ID_6NUM:
        {
            OnChar(hWnd, '6');
        }
        break;
    case ID_7NUM:
        {
            OnChar(hWnd, '7');
        }
        break;
    case ID_8NUM:
        {
            OnChar(hWnd, '8');
        }
        break;
    case ID_9NUM:
        {
            OnChar(hWnd, '9');
        }
        break;
    case ID_10NUM:
        {
            OnChar(hWnd, 'A');
        }
        break;
    case ID_11NUM:
        {
            OnChar(hWnd, 'B');
        }
        break;
    case ID_12NUM:
        {
            OnChar(hWnd, 'C');
        }
        break;
    case ID_13NUM:
        {
            OnChar(hWnd, 'D');
        }
        break;
    case ID_14NUM:
        {
            OnChar(hWnd, 'E');
        }
        break;
    case ID_15NUM:
        {
            OnChar(hWnd, 'F');
        }
        break;
    case ID_ESC:
        {
            HWND hParent = GetHosting(hWnd);
            if(hParent > 0)
            {
            }
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_ENTER:
        {
            HWND hParent = GetHosting(hWnd);
            SendMessage(hWnd, MSG_CLOSE, 0, 0);
            if(hParent > 0)
            {
                SendMessage(hParent, MSG_SETDATA, (WPARAM)g_edit_text, 0);
            }
        }
        break;
    case ID_DEL:
        {
            int len = strlen(g_edit_text);
            if(len > 0)
            {
                g_edit_text[len - 1] = 0;
                HWND hParent = GetHosting(hWnd);
                HWND hTmp = 0;
                if(hParent > 0)
                {
                    hTmp = GetFocusChild(hParent);
                }
                if(hTmp > 0)
                {
                    SetWindowCaption(hTmp, g_edit_text);
                    InvalidateRect(hTmp, NULL, TRUE);
                }
            }
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, ID_ENTER);
    }
    else if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_REMOVE || key == SCANCODE_BACKSPACE)
    {
        OnBtnCmd(hWnd, ID_DEL);
    }
}
/*
 *
 */
static int HexKeyWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateHexKeyInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_CHAR:
        {
            OnChar(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            if(lParam)
            {
                memcpy(&rect, (RECT*)lParam, sizeof(RECT));
            }
            else
            {
                GetClientRect(hWnd, &rect);
            }
            int width = GET_RECT_WIDTH(rect);
            int height = GET_RECT_HEIGHT(rect);
            SetBrushColor(hdc, COLOR_HQ_BLACK(hdc));
            FillBox(hdc, rect.left, rect.top, width, height);
            ReleaseDC(hdc);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateHexKeyWindow(HWND hHosting, RECT rect)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int cx = 159;
    int cy = 159;

    int x = rect.left + 10;
    if(rect.left + 15 + cx > CX_SCREEN)
    {
        x = __max(10, rect.left - 10 - cx);
    }
    int y = rect.bottom + 10;
    if(rect.bottom + 15 + cy > CY_SCREEN)
    {
        y = __max(10, rect.top - 10 - cy);
    }
    CreateInfo.dwStyle = WS_VISIBLE | WS_CHILD;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = HexKeyWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + cx;
    CreateInfo.by = y + cy;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd > 0)
    {
        g_edit_text[0] = 0;
        EnableWindow(hHosting, FALSE);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
